package com.demo.support.config;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import com.jweb.config.JWebConfig;
import com.jweb.config.Plugins;
import com.jweb.plugin.c3p0.C3p0Plugin;
import com.jweb.plugin.dynamicdatasource.DynamicDataSource;
//import com.jweb.plugin.jredis.RedisPlugin;
import com.jweb.plugin.mybatis.MybatisPlugin;
import com.jweb.plugin.mybatis.plugins.MybatisInterceptor_Page;
import com.jweb.plugin.mybatis.plugins.MybatisInterceptor_SqlLog;

//import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JConfig extends JWebConfig {
	private Logger log = LoggerFactory.getLogger(getClass());

	@Override
	public void configConstant(Map<String, String> constants) {
		// 系统常量配置区，可覆盖默认配置
		// constants.put(key, value);
	}

	@Override
	public void configPlugin(Plugins plugins) {
		// ==C3p0-mysql
		C3p0Plugin c3p0_mysql = new C3p0Plugin(getConstant("jdbc.url"), getConstant("jdbc.username"), getConstant("jdbc.password"),
				getConstant("jdbc.driverClass"), getConstantInt("jdbc.maxPoolSize"), getConstantInt("jdbc.minPoolSize"),
				getConstantInt("jdbc.initialPoolSize"), getConstantInt("jdbc.maxIdleTime"), getConstantInt("jdbc.acquireIncrement"));
		c3p0_mysql.start();
		plugins.add(c3p0_mysql);
		// ==C3p0-oracle
		C3p0Plugin c3p0_oracle = new C3p0Plugin(getConstant("jdbc2.url"), getConstant("jdbc2.username"), getConstant("jdbc2.password"),
				getConstant("jdbc2.driverClass"), getConstantInt("jdbc2.maxPoolSize"), getConstantInt("jdbc2.minPoolSize"),
				getConstantInt("jdbc2.initialPoolSize"), getConstantInt("jdbc2.maxIdleTime"), getConstantInt("jdbc2.acquireIncrement"));
		c3p0_oracle.start();
		plugins.add(c3p0_oracle);

		// ==动态数据源==============================================
		Map<String, DataSource> mapDB = new HashMap<String, DataSource>();
		mapDB.put("mysql_db", c3p0_mysql.getDataSource());
		mapDB.put("oracle_db", c3p0_oracle.getDataSource());
		DynamicDataSource dds = new DynamicDataSource(mapDB);

		// ==Mybatis
		MybatisPlugin mybatisPlugin = new MybatisPlugin(dds, "com.demo.dao", "sql.impl");
		mybatisPlugin.addPluginElement(new MybatisInterceptor_SqlLog());
		mybatisPlugin.addPluginElement(new MybatisInterceptor_Page("mysql"));
		mybatisPlugin.start();
		plugins.add(mybatisPlugin);
		// ==Jedis
		// RedisPlugin redisPlugin = new RedisPlugin("123.57.43.202", 6379,
		// "redis_1", "foobared");
		// redisPlugin.start();
		// plugins.add(redisPlugin);
		// // ==Dubbo-zookeeper
		// long times = System.currentTimeMillis();
		// log.info("SpringContext加载ing");
		// new ClassPathXmlApplicationContext(new String[] {
		// "application-dubbo-server.xml" }).start();
		// log.info("SpringContext加载完毕\t\t\t[{}]", new Object[] {
		// System.currentTimeMillis() - times });
	}

	public static void main(String[] args) {
		// new JettyServer("src/main/webapp", 8080, "/", 5).start();
	}
}
